﻿#include "systemmanagedialog.h"
#include "ui_systemmanagedialog.h"

SystemManageDialog::SystemManageDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SystemManageDialog),
    m_itemPerPage(8)
{
    ui->setupUi(this);
    setWindowFlags (Qt::Window | Qt::FramelessWindowHint);
    setWindowModality(Qt::ApplicationModal);

    this->move(0+g_mainWidgetPos.x(),0+g_mainWidgetPos.y());
    initDialog();
    initControl();

    m_logType = 0;

    m_curConnectWifi.clear();//当前连接的wifi
    m_curConnectWifiPass.clear();//当前连接的wifi密码
    m_curConnectMode = false;//当前连接的方式
    ui->labelCurIP->clear();

    ui->buttonSetAutoIP->setEnabled(false);
    ui->buttonSetStaticIP->setEnabled(false);
}

SystemManageDialog::~SystemManageDialog()
{
    delete ui;
}

//初始化窗体控件，包括位置、尺寸、样式
void SystemManageDialog::initControl()
{
    m_itemBtnList.clear();
    m_itemBtnList.append(ui->button1);
    m_itemBtnList.append(ui->button2);
    m_itemBtnList.append(ui->button3);
    m_itemBtnList.append(ui->button4);
    m_itemBtnList.append(ui->button5);
    ui->button6->setVisible(true);//显示
    ui->button7->setVisible(true);
    ui->button8->setVisible(true);
    m_itemBtnList.append(ui->button6);
    m_itemBtnList.append(ui->button7);
    m_itemBtnList.append(ui->button8);

    //根据不同分辨率设置控件的位置和尺寸
    initResolution();
    initControlStyle();//初始化窗体控件样式
}

//初始化窗体控件为任意分辨率
void SystemManageDialog::initResolution()
{
    double factoryX = getFactoryX();
    double factoryY = getFactoryY();

    this->resize(GLB_SCR_WIGHT*factoryX,GLB_SCR_HEIGHT*factoryY);
    ui->frameBack->setGeometry(0*factoryX,0*factoryY,GLB_SCR_WIGHT*factoryX,GLB_SCR_HEIGHT*factoryY);
    ui->buttonTypeLogo->setGeometry(GLB_EDGE_WIGHT*factoryX,GLB_EDGE_HEIGHT*factoryY,GLB_LICON_SIZE*factoryX,GLB_LICON_SIZE*factoryY);
    ui->labelMainTitle->setGeometry((GLB_EDGE_WIGHT+GLB_LICON_SIZE+GLB_EDGE_WIGHT)*factoryX,GLB_EDGE_HEIGHT*factoryY,
                                    (GLB_SCR_WIGHT-(GLB_EDGE_WIGHT+GLB_LICON_SIZE+GLB_EDGE_WIGHT*2))*factoryX,GLB_TEXT_L_HEIGHT*factoryY);
    ui->labelSubTitle->setGeometry((GLB_EDGE_WIGHT+GLB_LICON_SIZE+GLB_EDGE_WIGHT+GLB_TAB_WIGHT)*factoryX,(GLB_EDGE_HEIGHT+GLB_LICON_SIZE-GLB_TEXT_HEIGHT)*factoryY,
                                   (GLB_SCR_WIGHT-(GLB_EDGE_WIGHT+GLB_LICON_SIZE+GLB_EDGE_WIGHT+GLB_TAB_WIGHT*2))*factoryX,GLB_TEXT_HEIGHT*factoryY);


    ui->frameRightUp->setGeometry(0*factoryX,(GLB_EDGE_HEIGHT+GLB_LICON_SIZE-GLB_MICON_SIZE)*factoryY,GLB_SCR_WIGHT*factoryX,GLB_MICON_SIZE*factoryY);
    ui->buttonDelete->setGeometry(150*factoryX,0*factoryY,50*factoryX,50*factoryY);
    ui->buttonSelectAll->setGeometry(150*factoryX,0*factoryY,50*factoryX,50*factoryY);
    ui->buttonExport->setGeometry(90*factoryX,0*factoryY,50*factoryX,50*factoryY);

    ui->frameRightUpWIFI->setGeometry(600*factoryX,40*factoryY,400*factoryX,61*factoryY);
    ui->buttonRefresh->setGeometry(300*factoryX,0*factoryY,50*factoryX,50*factoryY);
    ui->labelCurIP->setGeometry(0*factoryX,0*factoryY,300*factoryX,50*factoryY);

    ui->buttonChangeLog->setGeometry(20*factoryX,0*factoryY,50*factoryX,50*factoryY);

    for(int i = 0; i < m_itemPerPage; i++)
    {
        m_itemBtnList[i]->setGeometry(GLB_PARA_LEFT*factoryX,(GLB_PARA_TOP + i * GLB_PARA_HEIGHT +3)*factoryY,GLB_PARA_WIGHT*factoryX,(GLB_PARA_HEIGHT-3)*factoryY);
    }

    ui->framePageBtn->setGeometry(0*factoryX,(GLB_SCR_HEIGHT-GLB_LBUT_HEIGHT-GLB_EDGE_WIGHT)*factoryY,GLB_SCR_WIGHT*factoryX,GLB_LBUT_HEIGHT*factoryY);
    ui->labelPage->setGeometry(GLB_EDGE_WIGHT*factoryX,(GLB_SCR_HEIGHT-GLB_TEXT_HEIGHT-GLB_EDGE_WIGHT)*factoryY,(GLB_SCR_WIGHT/3)*factoryX,GLB_TEXT_HEIGHT*factoryY);
    ui->labelExplain->setGeometry(130*factoryX,538*factoryY,350*factoryX,50*factoryY);
    ui->buttonPgUp->setGeometry((GLB_SCR_WIGHT-(GLB_EDGE_WIGHT+GLB_BAS_WIGHT*3+GLB_LBUT_WIGHT*4))*factoryX,0*factoryY,GLB_LBUT_WIGHT*factoryX,GLB_LBUT_HEIGHT*factoryY);
    ui->buttonPgDn->setGeometry((GLB_SCR_WIGHT-(GLB_EDGE_WIGHT+GLB_BAS_WIGHT*2+GLB_LBUT_WIGHT*3))*factoryX,0*factoryY,GLB_LBUT_WIGHT*factoryX,GLB_LBUT_HEIGHT*factoryY);
    ui->buttonOk->setGeometry((GLB_SCR_WIGHT-(GLB_EDGE_WIGHT+GLB_BAS_WIGHT*1+GLB_LBUT_WIGHT*2))*factoryX,0*factoryY,GLB_LBUT_WIGHT*factoryX,GLB_LBUT_HEIGHT*factoryY);
    ui->buttonCancel->setGeometry((GLB_SCR_WIGHT-(GLB_EDGE_WIGHT+GLB_BAS_WIGHT*0+GLB_LBUT_WIGHT*1))*factoryX,0*factoryY,GLB_LBUT_WIGHT*factoryX,GLB_LBUT_HEIGHT*factoryY);

    ui->framePageBtnWIFI->setGeometry(489*factoryX,545*factoryY,600*factoryX,45*factoryY);
    ui->buttonPgUpWIFI->setGeometry(0*factoryX,0*factoryY,96*factoryX,40*factoryY);
    ui->buttonPgDnWIFI->setGeometry(106*factoryX,0*factoryY,96*factoryX,40*factoryY);
    ui->buttonSetAutoIP->setGeometry(212*factoryX,0*factoryY,96*factoryX,40*factoryY);
    ui->buttonSetStaticIP->setGeometry(318*factoryX,0*factoryY,96*factoryX,40*factoryY);
    ui->buttonCancelWIFI->setGeometry(424*factoryX,0*factoryY,96*factoryX,40*factoryY);
}

//初始化窗体控件样式
void SystemManageDialog::initControlStyle()
{
    SetStyle setControlStyle;
    setControlStyle.setUiName(this->objectName());

    //背景图
    QString frameBackImgPath = setControlStyle.getSharedStyleSheet();
    ui->frameBack->setStyleSheet(frameBackImgPath);
    ui->labelMainTitle->setFont(fontSize_L());
    ui->labelMainTitle->setStyleSheet(titleTextColour());
    ui->labelSubTitle->setFont(fontSize_M());
    ui->labelSubTitle->setStyleSheet(noteTextColour());
    ui->labelPage->setFont(fontSize_M());
    ui->labelPage->setStyleSheet(commonTextColour());
    ui->labelExplain->setFont(fontSize_M());
    ui->labelExplain->setStyleSheet(titleTextColour());

    //右上方
    ui->frameRightUp->setStyleSheet(transparentStyle());
    ui->buttonDelete->setStyleSheet(backIconStyle());;
    ui->buttonDelete->setTopImage(setControlStyle.getTopStyleSheet(ui->buttonDelete->objectName()));
    ui->buttonSelectAll->setStyleSheet(backIconStyle());;
    ui->buttonSelectAll->setTopImage(setControlStyle.getTopStyleSheet(ui->buttonSelectAll->objectName()));
    ui->buttonExport->setStyleSheet(backIconStyle());;
    ui->buttonExport->setTopImage(setControlStyle.getTopStyleSheet(ui->buttonExport->objectName()));
    ui->buttonChangeLog->setStyleSheet(backIconStyle());;
    ui->buttonChangeLog->setTopImage(setControlStyle.getTopStyleSheet(ui->buttonChangeLog->objectName()));

    //右上方
    ui->frameRightUpWIFI->setStyleSheet(transparentStyle());
    ui->buttonRefresh->setStyleSheet(backIconStyle());;
    ui->buttonRefresh->setTopImage(setControlStyle.getTopStyleSheet(ui->buttonChangeLog->objectName()));
    ui->labelCurIP->setFont(fontSize_M());
    ui->labelCurIP->setStyleSheet(titleTextColour());

    for(int i = 0; i < m_itemBtnList.size(); i++)
    {
        m_itemBtnList[i]->setFont(fontSize_M());
        m_itemBtnList[i]->setStyleSheet(optionsIconStyle());
    }

    //四个按钮样式表一样
    ui->buttonPgUp->setStyleSheet(confirmIconStyle());
    ui->buttonPgUp->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonPgUp->objectName()),12);

    ui->buttonPgDn->setStyleSheet(confirmIconStyle());
    ui->buttonPgDn->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonPgDn->objectName()),12);

    ui->buttonOk->setStyleSheet(confirmIconStyle());
    ui->buttonOk->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonOk->objectName()),12);

    ui->buttonCancel->setStyleSheet(confirmIconStyle());
    ui->buttonCancel->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonCancel->objectName()),12);

    //WIFI
    ui->framePageBtnWIFI->setStyleSheet(transparentStyle());
    ui->buttonPgUpWIFI->setStyleSheet(confirmIconStyle());
    ui->buttonPgUpWIFI->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonPgUp->objectName()),12);

    ui->buttonPgDnWIFI->setStyleSheet(confirmIconStyle());
    ui->buttonPgDnWIFI->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonPgDn->objectName()),12);

    ui->buttonSetAutoIP->setStyleSheet(confirmIconStyle());
    ui->buttonSetAutoIP->setFont(fontSize_M());

    ui->buttonSetStaticIP->setStyleSheet(confirmIconStyle());
    ui->buttonSetStaticIP->setFont(fontSize_M());

    ui->buttonCancelWIFI->setStyleSheet(confirmIconStyle());
    ui->buttonCancelWIFI->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonCancel->objectName()),12);
}

void SystemManageDialog::refreshUi()
{
    int itemNum;
    if (m_itemList.size() <= 0)
    {
        itemNum = 0;

        for (int i = 0; i < m_itemPerPage; i++)
        {
            m_itemBtnList.at(i)->hide();
        }
        ui->labelPage->setText(tr("pageNum: 0/0"));//页数: 0/0
        ui->buttonPgUp->setEnabled(false);
        ui->buttonPgDn->setEnabled(false);
        ui->buttonOk->setEnabled(false);

        return;
    }
    else
    {
        itemNum = m_itemList.size();
    }

    m_pageNums = (itemNum + m_itemPerPage - 1)/m_itemPerPage;// 计算页数
    if (m_curPages > m_pageNums)// 当前页
    {
        m_curPages = m_pageNums;
    }
    if (m_curPages <= 1 && itemNum != 0)
    {
        m_curPages = 1;
    }

    int itemidx = (m_curPages - 1) * m_itemPerPage;

    for (int i = 0; i < m_itemPerPage; i++)
    {
        if (itemidx < itemNum)
        {
            //设置图标和文件夹名称
            m_itemBtnList[i]->setText(m_itemList.at(itemidx));
            m_itemBtnList.at(i)->show();

            itemidx++;
        }
        else
        {
            m_itemBtnList.at(i)->hide();
        }
    }

    // 翻页按钮
    if (m_curPages <= 1)
    {
        ui->buttonPgUp->setEnabled(false);
    }
    else
    {
        ui->buttonPgUp->setEnabled(true);
    }
    if (m_curPages >= m_pageNums)
    {
        ui->buttonPgDn->setEnabled(false);
    }
    else
    {
        ui->buttonPgDn->setEnabled(true);
    }

    // 页信息
    if(m_pageNums == 0)
    {
        m_curPages = 0;
    }

    QString str = tr("pageNum: ") + QString("%1/%2").arg(m_curPages).arg(m_pageNums);//页数:
    ui->labelPage->setText(str);

    refreshCheckedPage();
}

void SystemManageDialog::refreshCheckedPage()
{
    if(m_selectedIdx == SELECT_NONE)
    {
        ui->buttonOk->setEnabled(false);
    }
    else
    {
        ui->buttonOk->setEnabled(true);
    }

    if(m_selectedIdx == SELECT_ALL)
    {
        // 刷新页面中被选择的条目
        for(int var = 0; var < m_itemPerPage; var++)
        {
            // 条目被选中
            m_itemBtnList.at(var)->setCheckable(true);
            m_itemBtnList.at(var)->setChecked(true);
        }
        return;
    }

    // 刷新页面中被选择的条目
    for(int var = 0; var < m_itemPerPage; var++)
    {
        int idx = (m_curPages - 1) * m_itemPerPage + var;
        if(m_selectedIdx == idx)
        {
            // 条目被选中
            m_itemBtnList.at(var)->setCheckable(true);
            m_itemBtnList.at(var)->setChecked(true);
        }
        else
        {
            // 不被选中
            m_itemBtnList.at(var)->setChecked(false);
            m_itemBtnList.at(var)->setCheckable(false);
        }
    }
}

void SystemManageDialog::clickItem(int idx)
{
    m_selectedIdx = (m_curPages - 1) * m_itemPerPage + idx - 1;
    refreshCheckedPage();
    QString wifiName = m_itemList[m_selectedIdx];
    if(m_curConnectWifi != wifiName)
    {
        ui->buttonSetAutoIP->setEnabled(true);
        ui->buttonSetStaticIP->setEnabled(false);
    }
    else
    {
        ui->buttonSetAutoIP->setEnabled(false);
        ui->buttonSetStaticIP->setEnabled(true);
    }
}

void SystemManageDialog::initDialog(s16 type)
{
    m_setType = type;
    m_curPages = 1;//当前页数
    m_pageNums = 1;//总页数
    m_selectedIdx = SELECT_NONE;//被选中的条目
    m_itemList.clear();

    if(type != 0)
    {
        ui->frameRightUpWIFI->show();
        ui->framePageBtnWIFI->show();
        ui->frameRightUp->hide();
        ui->framePageBtn->hide();
        ui->labelExplain->show();
    }
    else
    {
        ui->frameRightUpWIFI->hide();
        ui->framePageBtnWIFI->hide();
        ui->frameRightUp->show();
        ui->framePageBtn->show();
        ui->labelExplain->hide();
    }
}

//设置类型图标
void SystemManageDialog::setTypeLogo(QString tStyle)
{
    ui->buttonTypeLogo->setUnPreBtnLogo(tStyle);
}

//设置主title
void SystemManageDialog::setMainTitle(QString str)
{
    ui->labelMainTitle->setText(str);
}

//设置副title
void SystemManageDialog::setSubTitle(QString str)
{
    ui->labelSubTitle->setText(str);
}

void SystemManageDialog::addItem(QString str)
{
    m_itemList.append(str);
}

void SystemManageDialog::addListError()
{
    m_itemList = g_pSettings->readToCsv(TYPE_ERROR);//读取   QStringList
}

void SystemManageDialog::addListBreakage()
{
    m_itemList = g_pSettings->readToCsv(TYPE_BREAK);//读取   QStringList
}

void SystemManageDialog::addListDebug()
{
    m_itemList = g_pSettings->readToCsv(TYPE_DEBUGINFO);//读取   QStringList
}

int SystemManageDialog::exec(int type)//升级类型
{
    ui->buttonDelete->setVisible(false);
    ui->buttonSelectAll->setVisible(false);
    ui->buttonExport->setVisible(false);

    ui->buttonChangeLog->setVisible(false);

    m_updateType = type;

    //当为外围板版本查询时，列表按钮和确定按钮不可按
    if(m_updateType < 0)
    {
        ui->buttonOk->setEnabled(false);

        for(int i = 0; i < m_itemBtnList.size(); i++)
        {
            m_itemBtnList[i]->setEnabled(false);
        }
    }
    else
    {
        if(m_updateType == PRODUCTSTATIS)
        {
            ui->buttonDelete->setVisible(true);
            ui->buttonOk->setEnabled(false);

            for(int i = 0; i < m_itemBtnList.size(); i++)
            {
                m_itemBtnList[i]->setEnabled(false);
            }
        }
        else if(m_updateType == JOURNAL)
        {
            ui->buttonDelete->setVisible(false);//不可见
            ui->buttonOk->setEnabled(false);
            ui->buttonExport->setVisible(true);

            ui->buttonChangeLog->setVisible(true);

            for(int i = 0; i < m_itemBtnList.size(); i++)
            {
                m_itemBtnList[i]->setEnabled(false);
            }
        }
        else
        {
            ui->buttonOk->setEnabled(true);

            for(int i = 0; i < m_itemBtnList.size(); i++)
            {
                m_itemBtnList[i]->setEnabled(true);
            }
        }
    }

    refreshUi();
    return QDialog::exec();
}

QString SystemManageDialog::getCurFile()
{
    QString fileName = m_itemList.at(m_selectedIdx);
    return fileName;
}

void SystemManageDialog::setWifiInfo(QString ip,QString ssid,QString psd,bool mode)
{
    ui->labelCurIP->setText(ip);
    m_curConnectWifi = ssid;
    m_curConnectWifiPass = psd;
    m_curConnectMode = mode;
}

void SystemManageDialog::setItemList(QStringList list)
{
    m_itemList.clear();
    m_itemList = list;
}

void SystemManageDialog::on_buttonCancel_clicked()
{
    //超级用户密码权限,单次有效
    g_emUser = operate;
    done(0);
}

void SystemManageDialog::on_buttonOk_clicked()
{
    if(m_updateType == HMI_UPDATA || m_updateType == BACKUPS_UPDATA)//界面升级 或者 界面还原
    {
        //升级需要重新启动程序，是否重启进行升级？
        qDebug()<<"HMI_UPDATA";

        PromptDialog promptDlg(this);
        promptDlg.initDialog(PromptDialog::BTN_OK_CANCEL);
        promptDlg.setTitleStr(tr("Prompt"));

        QString str;
        str = tr("The upgrade needs to restart the program. Do you need to restart to upgrade?");//升级需要重新启动程序，是否重启进行升级？
        promptDlg.setContentStr(str);
        if(promptDlg.exec() != 1)
        {
           //done(0);
           return;
        }
    }
    done(1);
}

void SystemManageDialog::on_buttonPgUp_clicked()
{
    m_curPages--;
    refreshUi();
}

void SystemManageDialog::on_buttonPgDn_clicked()
{
    m_curPages++;
    refreshUi();
}

void SystemManageDialog::on_button1_clicked()
{
    clickItem(1);
}

void SystemManageDialog::on_button2_clicked()
{
    clickItem(2);
}

void SystemManageDialog::on_button3_clicked()
{
    clickItem(3);
}

void SystemManageDialog::on_button4_clicked()
{
    clickItem(4);
}

void SystemManageDialog::on_button5_clicked()
{
    clickItem(5);
}

void SystemManageDialog::on_button6_clicked()
{
    clickItem(6);
}

void SystemManageDialog::on_button7_clicked()
{
    clickItem(7);
}

void SystemManageDialog::on_button8_clicked()
{
    clickItem(8);
}

void SystemManageDialog::on_buttonDelete_clicked()
{
    if(m_updateType == PRODUCTSTATIS)//等于生产统计
    {
        PromptDialog promptDlg(this);
        promptDlg.initDialog(PromptDialog::BTN_OK_CANCEL);
        promptDlg.setTitleStr(tr("Prompt"));

        QString str;
        str = tr("Do you want to clear the production statistics?");//是否清空生产统计？
        promptDlg.setContentStr(str);
        if(promptDlg.exec() == 1)
        {
            emit siClearProductStatis();
        }
    }
    else if(m_updateType == JOURNAL)
    {
        PromptDialog promptDlg(this);
        promptDlg.initDialog(PromptDialog::BTN_OK_CANCEL);
        promptDlg.setTitleStr(tr("Prompt"));

        QString str;
        str = tr("Do you want to clear the journal?");//是否清空日志文件？
        promptDlg.setContentStr(str);
        if(promptDlg.exec() == 1)
        {
            emit siClearJournal();
        }
    }
}

void SystemManageDialog::on_buttonSelectAll_clicked()
{
    if(m_selectedIdx == SELECT_ALL)
    {
        m_selectedIdx = SELECT_NONE;
    }
    else
    {
        m_selectedIdx = SELECT_ALL;
    }
    refreshCheckedPage();
}

//导出csv文件
void SystemManageDialog::on_buttonExport_clicked()
{
    if(m_updateType == JOURNAL)//等于日志文件
    {
        PromptDialog promptDlg(this);
        promptDlg.initDialog(PromptDialog::BTN_OK_CANCEL);
        promptDlg.setTitleStr(tr("Prompt"));

        QString str;
        str = tr("Do you want to export journal?");//是否导出日志文件？
        promptDlg.setContentStr(str);
        if(promptDlg.exec() == 1)
        {
            emit siCsvExport(m_logType);
        }
    }
}

void SystemManageDialog::on_buttonChangeLog_clicked()
{
    //done(0);
    //qDebug()<<"buttonChange_clicked" <<endl;
    m_logType++;
    if(m_logType > TYPE_DEBUGINFO)
    {
        m_logType = 0;
    }

     if(m_logType == TYPE_ERROR)
    {
         emit siCsvChangeErro();
    }
    else if(m_logType == TYPE_BREAK)
    {
         emit  siCsvChangeBrea();
    }
     else if(m_logType == TYPE_DEBUGINFO)
     {
          emit  siCsvChangeDebug();
     }

    refreshUi();
}

//刷新wifi
void SystemManageDialog::on_buttonRefresh_clicked()
{
    m_curPages = 1;//当前页数
    m_pageNums = 1;//总页数
    m_selectedIdx = SELECT_NONE;//被选中的条目
    emit siRefreshWifiList();
    refreshUi();
}

void SystemManageDialog::on_buttonPgUpWIFI_clicked()
{
    m_curPages--;
    refreshUi();
}

void SystemManageDialog::on_buttonPgDnWIFI_clicked()
{
    m_curPages++;
    refreshUi();
}

void SystemManageDialog::on_buttonSetAutoIP_clicked()
{
    if(m_selectedIdx < m_itemList.size() && m_selectedIdx >= 0)
    {
        QString ssid = m_itemList.at(m_selectedIdx);
        emit siSetDynamicIP(ssid);
    }
}

void SystemManageDialog::on_buttonSetStaticIP_clicked()
{
    QString ssid = m_curConnectWifi;
    QString psd = m_curConnectWifiPass;
    QStringList ipList = ui->labelCurIP->text().split(".", QString::SkipEmptyParts);
    QString ip;
    ip.clear();
    if(ipList.size() >= 3)
    {
        ip = ipList[0]+"."+ipList[1]+"."+ipList[2]+".";
    }

    emit siSetStaticIP(ssid,psd,ip);
}

void SystemManageDialog::on_buttonCancelWIFI_clicked()
{
    done(0);
}
